package com.zaoniao.enstudy.api.controller;

import com.zaoniao.enstudy.api.dto.VocabularyAddDto;
import com.zaoniao.enstudy.api.dto.VocabularyDelDto;
import com.zaoniao.enstudy.api.dto.VocabularyUpdDto;
import com.zaoniao.enstudy.api.vo.VocabularyVo;
import com.zaoniao.enstudy.domain.entity.Vocabulary;
import com.zaoniao.enstudy.domain.service.VocabularyService;
import com.zaoniao.enstudy.infracore.constants.AppConstant;
import com.zaoniao.enstudy.infracore.web.BaseResponse;
import com.zaoniao.enstudy.infracore.web.DataResponse;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Tag(name = "词汇表相关Api")
@RequestMapping("/vocabulary")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VocabularyController {

    private final VocabularyService vocabularyService;

    /**
     * 新增词汇表.
     *
     * @param vocabularyAddDto 新增词汇表入参
     * @return -
     */
    @Operation(summary = "新增词汇表")
    @PostMapping
    public BaseResponse addUserVocabulary(@Valid @RequestBody VocabularyAddDto vocabularyAddDto) {
        vocabularyService.addUserVocabulary(vocabularyAddDto);
        return DataResponse.ok("词汇表新增成功");
    }

    /**
     * 修改词汇表.
     *
     * @param vocabularyUpdDto 修改词汇表入参
     * @return -
     */
    @Operation(summary = "修改词汇表")
    @PutMapping
    public BaseResponse updUserVocabulary(@Valid @RequestBody VocabularyUpdDto vocabularyUpdDto) {
        vocabularyService.updUserVocabulary(vocabularyUpdDto);
        return DataResponse.ok("词汇表修改成功");
    }

    /**
     * 删除词汇表.
     *
     * @param vocabularyDelDto 删除词汇表入参
     * @return -
     */
    @Operation(summary = "删除词汇表", description = "删除词汇表以及该表下的所有单词，避免误删，采用逻辑删除")
    @DeleteMapping
    public BaseResponse delUserVocabulary(@Valid @RequestBody VocabularyDelDto vocabularyDelDto) {
        vocabularyService.delUserVocabulary(vocabularyDelDto);
        return DataResponse.ok("词汇表删除成功");
    }

    /**
     * 词汇表列表查询.
     *
     * @param userId 用户Id - 即OpenId
     * @param page   第几页
     * @param size   每页显示数据量
     * @return 词汇表列表信息
     */
    @Operation(summary = "词汇表列表查询", description = "下拉刷新，采用分页查询词汇表")
    @GetMapping
    public DataResponse<PagedGridResult<VocabularyVo>> vocabularyList(
            @Parameter(name = "userId", description = "用户唯一标志,即openId") @RequestParam String userId,
            @Parameter(name = "page", description = "第几页", allowEmptyValue = true)
            @RequestParam(value = "page", required = false) Integer page,
            @Parameter(name = "size", description = "每一页显示的条数", allowEmptyValue = true)
            @RequestParam(value = "size", required = false) Integer size) {
        if (page == null) {
            page = AppConstant.PAGE;
        }
        if (size == null) {
            size = AppConstant.SIZE;
        }
        PagedGridResult<Vocabulary> pagedGridResult = vocabularyService.getVocabularyPage(userId, page, size);
        return DataResponse.ok(VocabularyVo.Converter.INSTANCE.do2voPage(pagedGridResult));
    }
}
